package com.wing.api.add.simpleWorkOrder.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONArray;
import com.wing.api.add.simpleWorkOrder.BaseSimpleWorkOrderService;
import com.wing.cabinet.model.response.EnterpricceIdVO;
import com.wing.common.exception.BusinessException;
import com.wing.common.utils.JsonResult;
import com.wing.production.feign.ProductionClient;
import com.wing.production.workOrder.entity.SimpleWorkOrderDevice;
import com.wing.production.workOrder.request.ProductionProcessMaterialForm;
import com.wing.production.workOrder.request.SimpleWorkOrderForm;
import com.wing.production.workOrder.request.SimpleWorkOrderProcessForm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.LinkedHashMap;

/**
 * 标准工单接口
 */
@Service
@Slf4j
public class StandardSimpleWorkOrderServiceImpl implements BaseSimpleWorkOrderService {

    @Transactional
    @Override
    public void logicProcess(SimpleWorkOrderForm simpleWorkOrderForm, EnterpricceIdVO enterpricceIdVo, ProductionClient productionClient) throws IOException {
//        log.info("请求参数,{}", jsonObject.toJSONString());
        //此处需要做接口签名验证
        //sign = md5（参数1+参数2+参数3+key）
        String sourceSign = simpleWorkOrderForm.getSign(); // 签名值
        String code = simpleWorkOrderForm.getCode(); // 工单编号
        String skuCode = simpleWorkOrderForm.getSkuCode(); // 生产产品编号
        Integer productionQuantity = simpleWorkOrderForm.getProductionQuantity(); // 生产数量
        String remark = simpleWorkOrderForm.getRemark(); // 备注描述
        String creatorName = simpleWorkOrderForm.getCreatorName(); // 添加人
        if(StringUtils.isBlank(sourceSign)){
            throw new BusinessException("签名值为空！");
        }
        if(StringUtils.isBlank(code)){
            throw new BusinessException("工单编号为空！");
        }
        if(StringUtils.isBlank(skuCode)){
            throw new BusinessException("生产产品编号为空！");
        }
        if(null == productionQuantity){
            throw new BusinessException("生产数量为空！");
        }
        if(StringUtils.isBlank(creatorName)){
            throw new BusinessException("添加人为空！");
        }
        // 工序集合
        JSONArray processFormsArr = new JSONArray();
        for (SimpleWorkOrderProcessForm simpleWorkOrderProcessForm : simpleWorkOrderForm.getProcessFormsList()) {
            if(null == simpleWorkOrderProcessForm.getProcessId()){
                throw new BusinessException("工序ID为空！");
            }
            // 设备集合
            JSONArray deviceList = new JSONArray();
            for (SimpleWorkOrderDevice simpleWorkOrderDevice : simpleWorkOrderProcessForm.getDeviceList()) {
                if(StringUtils.isBlank(simpleWorkOrderDevice.getDeviceCode())){
                    throw new BusinessException("设备编号为空！");
                }
                if(null == simpleWorkOrderDevice.getProductionQuantity()){
                    throw new BusinessException("计划生产数量为空！");
                }
                LinkedHashMap<String, Object> deviceMap = new LinkedHashMap<>();
                deviceMap.put("deviceCode", simpleWorkOrderDevice.getDeviceCode());
                deviceMap.put("productionQuantity", simpleWorkOrderDevice.getProductionQuantity());
                deviceList.add(deviceMap);
            }
            // 物料集合
            JSONArray materialList = new JSONArray();
            for (ProductionProcessMaterialForm productionProcessMaterialForm : simpleWorkOrderProcessForm.getMaterialList()) {
                if(null == productionProcessMaterialForm.getQuantity()){
                    throw new BusinessException("预计所需数量为空！");
                }
                if(StringUtils.isBlank(productionProcessMaterialForm.getMaterialSkuCode())){
                    throw new BusinessException("物料编号为空！");
                }
                LinkedHashMap<String, Object> materialMap = new LinkedHashMap<>();
                materialMap.put("quantity", productionProcessMaterialForm.getQuantity());
                materialMap.put("materialSkuCode", productionProcessMaterialForm.getMaterialSkuCode());
                materialList.add(materialMap);
            }
            // 工序
            LinkedHashMap<String, Object> processMap = new LinkedHashMap<>();
            processMap.put("processId", simpleWorkOrderProcessForm.getProcessId());
            processMap.put("deviceList", deviceList);
            processMap.put("materialList", materialList);
            processFormsArr.add(processMap);
        }
        StringBuffer tempStr = new StringBuffer();
        tempStr.append(code);
        tempStr.append(skuCode);
        tempStr.append(productionQuantity);
        if(StringUtils.isNotBlank(remark)){
            tempStr.append(remark);
        }
        tempStr.append(creatorName);
        tempStr.append(processFormsArr.toString());
        tempStr.append(enterpricceIdVo.getKey());
        log.info("签名源串,{}", sourceSign);
        String desSign = DigestUtil.md5Hex(tempStr.toString());
        log.info("签名目的串,{}", desSign);
        if(!desSign.equals(sourceSign)){
            throw new BusinessException("签名不通过！");
        }
        JsonResult jsonResult = productionClient.addSimpleWorkOrder(simpleWorkOrderForm);
        if(jsonResult.getCode() != 200){
            throw new BusinessException(jsonResult.getMsg());
        }
    }

    public static void main(String[] args) {
        String key = "$2a$10$fxpgo/SLFHCeNThnfF/q7uvoZJOxbJBNNI4.dshzlntgBIHKI9eb."; // 测试

        // 设备集合
        JSONArray deviceList = new JSONArray();
        LinkedHashMap<String, Object> deviceMap = new LinkedHashMap<>();
        deviceMap.put("deviceCode", "T-01");
        deviceMap.put("productionQuantity", 1);
        deviceList.add(deviceMap);

        // 物料集合
        JSONArray materialList = new JSONArray();
        LinkedHashMap<String, Object> materialMap = new LinkedHashMap<>();
        materialMap.put("quantity", 1);
        materialMap.put("materialSkuCode", "D1.4*5H*2E*D4*50L");
        materialList.add(materialMap);

        // 工序集合
        JSONArray processFormsList = new JSONArray();
        LinkedHashMap<String, Object> processMap = new LinkedHashMap<>();
        processMap.put("processId", 24);
        processMap.put("deviceList", deviceList);
        processMap.put("materialList", materialList);
        processFormsList.add(processMap);
        String desSign2= DigestUtil.md5Hex("GD-002" + "C0003-002" + "1" + "admin" + processFormsList + key);
        System.out.println(desSign2);
    }
}
